package com.youfang.sixthmedia;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.gzit.common.async.AsynCallback;
import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.message.LoginRequest;
import com.youfang.sixthmedia.message.LoginResponse;
import com.youfang.sixthmedia.message.center.FindpasswordDelegate;
import com.youfang.sixthmedia.message.model.User;
import com.youfang.sixthmedia.net.Net;
import com.youfang.sixthmedia.net.Response;
import com.youfang.sixthmedia.ui.center.CRegActivity;

public class LoginActivity extends Activity {

	OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.loginBtn:
				remoteLogin();
				break;
			case R.id.regBtn:
				remoteReg();
				break;
			case R.id.forgetBtn:
				remoteForget();
				break;
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.login_activity);
		for (int id : new int[] { R.id.loginBtn, R.id.regBtn, R.id.forgetBtn }) {
			this.findViewById(id).setOnClickListener(listener);
		}

		// test...
		// ((TextView) this.findViewById(R.id.username)).setText("teddy");
		// ((TextView) this.findViewById(R.id.password)).setText("388880");

	}

	private void remoteLogin() {
		String userName = ((TextView) this.findViewById(R.id.username))
				.getText().toString();

		final String password = ((TextView) this.findViewById(R.id.password))
				.getText().toString();

		if (StringUtils.isBlank(userName)) {
			AndroidUtils.toastShort("用户名不能为空");
			return;
		}

		if (StringUtils.isBlank(password)) {
			AndroidUtils.toastShort("密码不能为空");
			return;
		}

		LoginRequest req = new LoginRequest();
		req.setUsername(userName);
		req.setPassword(password);

		Net.doHttpRemote(this, req, new LoginResponse(),
				new AsynCallback<Response>() {

					@Override
					public void callback(Response r) {
						if (!r.isOk()) {
							AndroidUtils.toastShort("登录失败");
							return;
						}

						// 登录成功...
						LoginResponse resp = (LoginResponse) r;
						User user = resp.getUser();

						// save user infomation
						user.password = password;
						YoufangContext.saveUserAndStartMessageService(user);

						// 返回
						setResult(Activity.RESULT_OK);
						finish();

					}
				});

	}

	private final int REG_CODE = 1;

	private void remoteReg() {

		Intent intent = new Intent();
		intent.setClass(this, CRegActivity.class);
		this.startActivityForResult(intent, REG_CODE);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != Activity.RESULT_OK) {
			return;
		}

		switch (requestCode) {
		case REG_CODE:
			this.finish();
			break;

		}

	}

	private void remoteForget() {

		String userName = ((TextView) this.findViewById(R.id.username))
				.getText().toString();
		if (StringUtils.isBlank(userName)) {
			AndroidUtils.toastShort("用户名不能为空");
			return;
		}

		FindpasswordDelegate delegate = new FindpasswordDelegate();
		delegate.req.username = userName;

		Net.message(this, delegate, new AsynCallback<Response>() {

			@Override
			public void callback(Response r) {
				if (r.isFail()) {
					AndroidUtils.toastShort("找回密码失败");
					return;
				}

				AndroidUtils.toastShort("密码已重置并发送到您的注册邮箱。");

			}
		});

	}

}
